package com.ikunikun.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ikunikun.constant.MessageConstant;
import com.ikunikun.constant.StatusConstant;
import com.ikunikun.dto.DishDTO;
import com.ikunikun.dto.DishPageQueryDTO;
import com.ikunikun.entity.Dish;
import com.ikunikun.entity.DishFlavor;
import com.ikunikun.entity.Setmeal;
import com.ikunikun.exception.DeletionNotAllowedException;
import com.ikunikun.mapper.DishFlavorMapper;
import com.ikunikun.mapper.DishMapper;
import com.ikunikun.mapper.SetmealDishMapper;
import com.ikunikun.mapper.SetmealMapper;
import com.ikunikun.result.PageResult;
import com.ikunikun.service.DishService;
import com.ikunikun.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DishServiceImpl implements DishService {

	@Resource
	private DishMapper dishMapper;
	@Resource
	private DishFlavorMapper dishFlavorMapper;
	@Resource
	private SetmealDishMapper setmealDishMapper;
	@Resource
	private SetmealMapper setmealMapper;
	@Resource
	private RedisTemplate redisTemplate;

	/**
	 * 新增菜品
	 */
	@Override
	@Transactional
	public void saveWithFlavor(DishDTO dishDTO) {
		Dish dish = new Dish();
		BeanUtils.copyProperties(dishDTO, dish);

		// 向菜品表插入一条数据
		dishMapper.insert(dish);
		// 向口味表插入N条数据
		List<DishFlavor> flavors = dishDTO.getFlavors();
		if (Objects.isNull(flavors) || flavors.isEmpty()){
			return;
		}
		Long id = dish.getId();
		flavors = flavors.stream().map(dishFlavor -> {
			dishFlavor.setDishId(id);
			return dishFlavor;
		}).collect(Collectors.toList());

		dishFlavorMapper.insertBatch(flavors);
	}

	/**
	 * 菜品分页查询
	 * @param dishPageQueryDTO
	 * @return
	 */
	@Override
	public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
		PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
		Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
		return new PageResult(page.getTotal(),page.getResult());
	}

	/**
	 * 批量删除菜品
	 * @param ids
	 */
	@Override
	@Transactional
	public void deleteBatch(List<Long> ids) {
		// 判断当前菜品是否可以删除，起售中无法删除
		for (Long id : ids) {
			Dish dish = dishMapper.getById(id);
			if (dish.getStatus() == StatusConstant.ENABLE) {
				throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
			}
		}
		// 判断菜品是否关联套餐，关联无法删除
		List<Long> setMealIds = setmealDishMapper.getSetMealIdsByDishIds(ids);
		if (!Objects.isNull(setMealIds) && !setMealIds.isEmpty()){
			throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
		}

		// 删除菜品表中的菜品数据
		// 删除菜品关联的口味数据
		for (Long id : ids) {
			dishMapper.deleteById(id);
			dishFlavorMapper.deleteByDishId(id);
		}

	}

	/**
	 * 根据id查询菜品信息
	 * @param id
	 * @return
	 */
	@Override
	public DishVO getByIdWithFlavor(Long id) {
		// 根据id查询菜品信息
		Dish dish = dishMapper.getById(id);
		DishVO dishVO = new DishVO();
		BeanUtils.copyProperties(dish, dishVO);

		// 根据dishId查询口味数据
		List<DishFlavor> dishFlavorList = dishFlavorMapper.getByDishId(id);

		dishVO.setFlavors(dishFlavorList);
		return dishVO;
	}

	/**
	 * 修改菜品信息
	 * @param dishDTO
	 */
	@Override
	@Transactional
	public void updateWithFlavor(DishDTO dishDTO) {
		// 修改菜品基本信息
		Dish dish = new Dish();
		BeanUtils.copyProperties(dishDTO, dish);
		dishMapper.update(dish);
		Long dishId = dish.getId();
		// 删除原有的口味信息
		dishFlavorMapper.deleteByDishId(dishId);
		// 重新插入口味信息
		List<DishFlavor> flavors = dishDTO.getFlavors();
		if (Objects.isNull(flavors) || flavors.isEmpty()){
			return;
		}
		flavors = flavors.stream().map(dishFlavor -> {
			dishFlavor.setDishId(dishId);
			return dishFlavor;
		}).collect(Collectors.toList());
		dishFlavorMapper.insertBatch(flavors);
	}

	/**
	 * 根据categoryId查询菜品
	 * @param categoryId
	 * @return
	 */
	@Override
	public List<DishVO> getByCategoryId(Long categoryId) {
		return dishMapper.getByCategoryId(categoryId);
	}

	/**
	 * 修改菜品状态
	 * @return
	 */
	@Override
	public void stopOrStart(Integer status, Long id) {
		if(status.equals(StatusConstant.DISABLE)) {
			List<Long> arrayList = new ArrayList<>();
			arrayList.add(id);
			List<Long> setMealIds = setmealDishMapper.getSetMealIdsByDishIds(arrayList);

			Setmeal setmeal = Setmeal.builder().status(StatusConstant.DISABLE).build();
			for (Long setmealId : setMealIds) {
				setmeal.setId(setmealId);
				setmealMapper.update(setmeal);
			}
		}

		Dish dish = Dish.builder()
				.status(status)
				.id(id)
				.build();
		dishMapper.update(dish);
	}

	/**
	 * 据口味id查询菜品
	 * @param categoryId
	 *
	 * @return
	 */
	@Override
	public List<DishVO> listWithFlavor(Long categoryId) {
		String key = "DishByCategoryId:" + categoryId;

		Dish dish = Dish.builder()
			.status(StatusConstant.ENABLE)
			.categoryId(categoryId)
			.build();

		List<Dish> dishList = dishMapper.listWithCategoryId(dish);

		List<DishVO> dishVOS = new ArrayList<>();

		for (Dish one : dishList) {
			DishVO dishVO = new DishVO();
			BeanUtils.copyProperties(one, dishVO);
			// 根据菜品id查询口味
			List<DishFlavor> flavors = dishFlavorMapper.getByDishId(one.getId());
			dishVO.setFlavors(flavors);
			dishVOS.add(dishVO);

		}
		return dishVOS;
	}
}
